package com.labo.erpcrm.controleur;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.labo.erpcrm.bean.Client;
import com.labo.erpcrm.dao.ClientDao;
import com.labo.erpcrm.metier.EditClientBusiness;

public class EditClientControleur extends HttpServlet {

	private static final long serialVersionUID = 1L;
	private static final String VUE_NOUVEAUCLIENT = "/ListClientsControleur";
	private static final String VUE_EDIT_CLIENT = "/client-edit.jsp";
	protected EditClientBusiness editClient;
	protected Client client;
	protected ClientDao clientDao;

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		RequestDispatcher rd = null;
		editClient = new EditClientBusiness();
		client = new Client();
		int client_id;
		String str_client_id = req.getParameter("id").trim();
		if(str_client_id != null){
			client_id = Integer.parseInt(str_client_id);
			client = editClient.getById(client_id);
			rd = getServletContext().getRequestDispatcher(VUE_EDIT_CLIENT);
			req.setAttribute("edit_client", editClient);
			req.setAttribute("client", client);
		}
		rd.forward(req, resp);
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		System.out.println("DO POST");
		RequestDispatcher rd = null;
		editClient = new EditClientBusiness();
		editClient.validateEditClient(req);
		rd = getServletContext().getRequestDispatcher(VUE_NOUVEAUCLIENT);
		req.setAttribute("edit_client", editClient);
		rd.forward(req, resp);

	}

}
